package com.sxz.sbs.app;

import android.app.Activity;
import android.app.Application;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.sxz.sbs.http.retrofit.RetrofitHelper;
import com.sxz.sbs.http.retrofit.StallBusinessApi;
import com.sxz.sbs.util.PreferencesUtil;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * 程序入口
 *
 * @author hp
 *
 */

public class App extends Application {
    private static StallBusinessApi businessApi;
    /**
     * 存放Activity的集合
     */
    private Set<Activity> activityList;
    /**
     * 偏好设置
     */
    public static PreferencesUtil preferences;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("app", "app启动。。。。");
        initApp();
        initJPush();
    }

    /**
     * 初始化推送
     */
    private void initJPush() {
        // 设置开启日志,发布时请关闭日志
        JPushInterface.setDebugMode(AppConstant.isOpenLog);
        // 初始化 JPush
        JPushInterface.init(this);
    }

    /**
     * 初始化
     */
    private void initApp() {
        // 检测内存是否泄漏.
        //LeakCanary.install(this);

        // 工具包
        Utils.init(this);
        // 关闭打印
        LogUtils.getConfig().setLogSwitch(AppConstant.isOpenLog);
        LogUtils.getConfig().setGlobalTag("TAG ---->");

        // ARouter 设置
        ARouter.openLog();
        // 因为Android studio 使用了instant run ，ARouter 跳转失效，所以开发时打开debug， release时关闭
//        ARouter.openDebug();
        ARouter.init(this);
        preferences = new PreferencesUtil(AppConstant.Preference.PREFERENCE_NAME);
        //  网络
        initHttpClient();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        GlideApp.get(this).clearMemory();
    }

    /**
     * 单例 模式
     *
     * @return
     */
    public static App getInstance() {
        return AppInstanceHold.APP;
    }

    private static class AppInstanceHold {
        private static final App APP = new App();
    }

    private void initHttpClient() {
        OkHttpClient okHttpClient = RetrofitHelper.getOkHttpClient(getApplicationContext());
        businessApi = RetrofitHelper.initRetrofit(okHttpClient, StallBusinessApi.class);

        OkHttpClient.Builder builder = okHttpClient.newBuilder();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(interceptor);

    }

    public StallBusinessApi getApi() {
        if (businessApi != null) {
            return businessApi;
        }
        throw new NullPointerException("StallBusinessApi object can not null");
    }

    /**
     * 退出APP
     */
    public void exitApp() {
        if (activityList != null) {
            synchronized (activityList) {
                for (Activity activity : activityList) {
                    activity.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * 清空activityList
     */
    public void clearActivity() {
        if (activityList != null) {
            synchronized (activityList) {
                for (Activity activity : activityList) {
                    activity.finish();
                }
            }
        }
    }

    /**
     * 添加activity
     *
     * @param activity 要添加的activity
     */
    public void addActivity(Activity activity) {
        if (activityList == null) {
            activityList = new HashSet<>();
        }
        activityList.add(activity);
    }

    /**
     * 移除指定的activity
     *
     * @param activity 要移除的activity
     */
    public void removeActivity(Activity activity) {
        if (activityList != null) {
            activityList.remove(activity);
            Log.e("EXIT", "退出了" + activity.getLocalClassName());
        }
    }
}
